package tests.DAOBag;

import org.nanotek.tests.TestException;
import org.nanotek.cms.domain.lucene.config.IndexConfig;
import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 * TODU: Change on near Future fora JUnit test.
 * A => What to test.
 * H => How to test.
 * W => When to test.
 * @author josecanovamauger
 * <A,H,W>
 */


public class DAOBagTest{

	private ClassPathXmlApplicationContext ctx;

	public DAOBagTest() {}

	public static void main(String args[])
	{
		DAOBagTest ict = new DAOBagTest();
		ict.tearUpTest();
		ict.testGeneralDAOOperations();
		ict.tearDownHibernate();
	}


	@SuppressWarnings("unused")
	public void tearUpTest(){
		try {
				ctx = new ClassPathXmlApplicationContext(new String[] {"tests/IndexConfig/IndexConfigXmlFile.xml"});
		}catch(Exception e)
		{
			//Nice article from Oracle.
			throw new TestException(e);
		}
	}

	public void testIndexConfigWithDao()
	{
		testGeneralDAOOperations();
	}

	private void testGeneralDAOOperations() {
		testCreateOperation();
		testUpdateOperation();
		testDeleteOperation();
	}


	@SuppressWarnings("unchecked")
	private void testCreateOperation() {

		IndexConfigSpringBuilder<String, IndexConfig> indexConfigSpringBuilder;indexConfigSpringBuilder = (IndexConfigSpringBuilder<String,IndexConfig>) ctx.getBean("indexConfigSpringBuilder");
		IndexConfig indexConfig = (IndexConfig) indexConfigSpringBuilder.createBeanInstance("indexConfig", new IndexConfig());
        checkIndexConfigConstraints(indexConfig);
	}

	private void checkIndexConfigConstraints(IndexConfig indexConfig) {
		// TODO Auto-generated method stub
	}

	@SuppressWarnings("unchecked")
	private void testUpdateOperation() {
		 IndexConfigSpringBuilder<String, IndexConfig> indexConfigSpringBuilder;
		 indexConfigSpringBuilder = (IndexConfigSpringBuilder<String,IndexConfig>) ctx.getBean("indexConfigSpringBuilder");
		 IndexConfig indexConfig = (IndexConfig) indexConfigSpringBuilder.createBeanInstance("indexConfig", new IndexConfig());
		 indexConfigSpringBuilder.persistBean("IndexConfigDAO", indexConfig);
		 assert (indexConfig.getId()!=null);
	}

	private void testDeleteOperation() {
		IndexConfigSpringBuilder<String, IndexConfig> indexConfigSpringBuilder;
		indexConfigSpringBuilder = (IndexConfigSpringBuilder<String,IndexConfig>) ctx.getBean("indexConfigSpringBuilder");
		IndexConfig indexConfig = (IndexConfig) indexConfigSpringBuilder.createBeanInstance("indexConfig", new IndexConfig());
		indexConfigSpringBuilder.deleteBean("IndexConfigDAO", indexConfig);
	}

	public void tearDownHibernate(){
		ctx.close();
	}
}



